package com.techysky.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Path;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import com.techysky.entity.SysLog;
import com.techysky.repository.SysLogRepository;
import com.techysky.service.SysLogService;

/**
 * 日志查询
 * @author zhangxu
 *
 */
@Service("sysLogService")
public class SysLogServiceImpl implements SysLogService{
	private static final String CREATE_DATE = "createDate";
	private static final long serialVersionUID = 1L;	
	@Autowired
	transient SysLogRepository syslogRepository;

	@Override
	public Object saveOrUpdate(SysLog log) {
		return this.syslogRepository.save(log);
	}
	@Override
	public Map<String,Object> query(SysLog log) {
		Map<String,Object> result=new HashMap<>();
		log.setPage(log.getPage()!=null?log.getPage():0);
		log.setSize(log.getSize()!=null?log.getSize():8);
		Page<SysLog> resultList=this.syslogRepository.findAll(queryConditions(log),new PageRequest(log.getPage(),log.getSize()));
		result.put("page", log.getPage()+1);
		result.put("size", log.getSize());
		result.put("rows", resultList.getContent());
		result.put("total", resultList.getTotalElements());
		result.put("log",log);
		return result;
	}/**
	 * 等值匹配条件
	 * @return
	 */
	public Specification<SysLog> queryConditions(final SysLog obj){
		return (Root<SysLog> root,CriteriaQuery<?> query, CriteriaBuilder cb)->{
            List<Predicate> predicate = new ArrayList<>();
            if(obj!=null&&obj.getUserName()!=null&&!"".equals(obj.getUserName())) {
				Path<String> userName = root.get("userName");
				Predicate p = cb.like(userName, "%" + obj.getUserName() + "%");
				predicate.add(p);
			}
            extract1(obj, root, cb, predicate);
            query.orderBy(cb.desc(root.get(CREATE_DATE)));
            Predicate[] pre = new Predicate[predicate.size()];
            return query.where(predicate.toArray(pre)).getRestriction();
		};
	}
	private void extract1(final SysLog obj, Root<SysLog> root, CriteriaBuilder cb, List<Predicate> predicate) {
		if(obj!=null&&obj.getType()!=null&&!"".equals(obj.getType())) {
			Path<String> type = root.get("type");
			Predicate p = cb.equal(type, obj.getType());
			predicate.add(p);
		}
		if(obj!=null&&obj.getOperation()!=null&&!"".equals(obj.getOperation())) {
			Path<String> operation = root.get("operation");
			Predicate p = cb.like(operation, "%" + obj.getOperation() + "%");
			predicate.add(p);
		}
		extract2(obj, root, cb, predicate);
	}
	private void extract2(final SysLog obj, Root<SysLog> root, CriteriaBuilder cb, List<Predicate> predicate) {
		if(obj!=null&&obj.getStartDate()!=null&&!"".equals(obj.getStartDate())) {
			Path<String> createDate = root.get(CREATE_DATE);
			Predicate p = cb.greaterThanOrEqualTo(createDate,obj.getStartDate());
			predicate.add(p);
		}
		if(obj!=null&&obj.getEndDate()!=null&&!"".equals(obj.getEndDate())) {
			Path<String> createDate = root.get(CREATE_DATE);
			Predicate p = cb.lessThanOrEqualTo(createDate, obj.getEndDate());
			predicate.add(p);
		}
	}}
